package org.edu.modules.classSchedule.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.edu.common.api.vo.Result;
import org.edu.common.system.query.QueryGenerator;
import org.edu.common.util.oConvertUtils;
import org.edu.modules.classSchedule.entity.VeJwPaike;
import org.edu.modules.classSchedule.service.IVeJwPaikeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.edu.modules.classSchedule.vo.ScheduleInfo;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.edu.common.system.base.controller.BaseController;
import org.quartz.ScheduleBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.edu.common.aspect.annotation.AutoLog;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @Description: 排课信息
 * @Author:
 * @Date:   2021-05-13
 * @Version: V1.0
 */
@Api(tags="排课信息")
@RestController
@RequestMapping("/classSchedule/veJwPaike")
@Slf4j
public class VeJwPaikeController extends BaseController<VeJwPaike, IVeJwPaikeService> {
	@Autowired
	private IVeJwPaikeService veJwPaikeService;
	
	/**
	 * 分页查询
	 * @param veJwPaike
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "排课信息-分页查询")
	@ApiOperation(value="查询信息", notes="排课信息-分页查询")
	@PostMapping(value = "/list")
	public Result<?> queryPageList(@RequestBody VeJwPaike veJwPaike,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="99") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<VeJwPaike> queryWrapper = QueryGenerator.initQueryWrapper(veJwPaike, req.getParameterMap());
		Page<VeJwPaike> page = new Page<VeJwPaike>(pageNo, pageSize);
		IPage<VeJwPaike> pageList = veJwPaikeService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 * 新增信息
	 * @param veJwPaike
	 * @return
	 */
	@AutoLog(value = "排课信息-新增信息")
	@ApiOperation(value="新增信息", notes="排课信息-新增信息")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody VeJwPaike veJwPaike) {
		String checkMsg = veJwPaikeService.checkNew(veJwPaike);
		if(!checkMsg.equals("")){
			return Result.error(checkMsg);
		}
		if(veJwPaikeService.save(veJwPaike)){
		   return Result.OK("添加成功！");
        }
        return Result.error("添加失败!");
	}
	
	/**
	 * 修改信息
	 * @param veJwPaike
	 * @return
	 */
	@AutoLog(value = "排课信息-修改信息")
	@ApiOperation(value="修改信息", notes="排课信息-修改信息")
	@PostMapping(value = "/edit")
	public Result<?> edit(@RequestBody VeJwPaike veJwPaike) {
        if(veJwPaikeService.updateById(veJwPaike)){
		    return Result.OK("编辑成功!");
        }
        return Result.error("编辑失败!");
	}

	 /**
	  * 自动排课
	  * @param
	  * @return
	  */
	 @AutoLog(value = "排课信息-自动排课")
	 @ApiOperation(value="自动排课", notes="排课信息-自动排课")
	 @PostMapping(value = "/autoSchedule")
	 public Result<?> autoSchedule(@RequestBody ScheduleInfo scheduleInfo) {
	 	if(scheduleInfo.getSemId()==null || scheduleInfo.getXqmc()==null || scheduleInfo.getXqmc().equals("")){
			return Result.error("请选择学期！");
		}
	 	String ret = veJwPaikeService.autoSchedule(scheduleInfo);
	 	if(ret.startsWith("OK:")){
			return Result.OK("",ret.substring(3));
		}
		 return Result.error(ret);
	 }

	 /**
	  * 分页查询
	  * @param veJwPaike
	  * @param req
	  * @return
	  */
	 @AutoLog(value = "排课信息-已排课列表")
	 @ApiOperation(value="已排课列表", notes="排课信息-已排课列表")
	 @PostMapping(value = "/scheduleCount")
	 public Result<?> scheduleCount(@ApiIgnore VeJwPaike veJwPaike,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									HttpServletRequest req) {
		 QueryWrapper<VeJwPaike> queryWrapper = QueryGenerator.initQueryWrapper(veJwPaike, req.getParameterMap());
		 Page<Map<String,Object>> page = new Page<Map<String,Object>>(pageNo, pageSize);
		 IPage<Map<String, Object>> pageList = veJwPaikeService.pageMaps(page,queryWrapper);
		 return Result.OK(pageList);

	 }


}
